package com.company.project.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.company.project.bean.Role;
import com.company.project.bean.User;
import com.company.project.service.SaveUserService;
import com.company.project.service.UserRoleService;
import com.company.project.service.UserService;
import com.company.project.util.VerifyCode;
import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private UserRoleService userRoleService;
	private SaveUserService saveUserService;
	private String username;
	private String password;
	private List<?> grantlist;
	private List<?> list;
	 User user1 = new User();
	


	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public void setSaveUserService(SaveUserService saveUserService) {
		this.saveUserService = saveUserService;
	}

	public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}

	public List<?> getGrantlist() {
		return grantlist;
	}

	public void setGrantlist(List<?> grantlist) {
		this.grantlist = grantlist;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
	public String checklogin() throws Exception {
		// TODO Auto-generated method stub
		User user = userService.checkLogin(username,password);
		if(user!=null&&user.getRole().getId()==1){
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("user", user);
			return SUCCESS;
		}else
			return ERROR;
	}
	
	public String verifycode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		VerifyCode.getVerifyCode(request, response);
		return null;
	}
	
	public String userlist(){
		list = userService.findAll();
		return "success";
	}
	
	public String insert(){
		/*grantlist=userRoleService.findAll();*/
		return "success";
	}
	
	public String save(){
		Role role = saveUserService.getUserRole(3);
		user1.setRole(role);
		saveUserService.save(user1);
		return SUCCESS;
	}

	public String modify(){
	user1 = userService.find(user1.getId());
	grantlist=userRoleService.findAll();
		return SUCCESS;
	}
	
	public String update(){
		  userService.modify(user1);
			return SUCCESS;
		}
	
	public String deleteuser(){
		userService.delete(user1.getId());
		return SUCCESS;
	}
	
	

	

	public String logout(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("user");
		return SUCCESS;
	}


	
	
/*	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user1;
	}
*/



	
	
}
